package com.jintoufs.util;

import com.jhj.common.util.JhjEncryptUtil;

import java.io.InputStream;
import java.util.*;

public class SecretKeyUtils {

	private static Properties properties = new Properties();

	private static String REST_API_ACCESS_PERMISSION = "apiaccesspermission";

	private static String secretKey;

	static {
		try {
			InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("applicationContext.properties");
			properties.load(fis);
			secretKey = properties.getProperty("secret.key");
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* 生成签名 */
	public static String getSign(Map<String, String> paramsMap) {
		Set<String> keySet = paramsMap.keySet();
		String[] keys = keySet.toArray(new String[keySet.size()]);

		Arrays.sort(keys);

		StringBuffer sb = new StringBuffer();
		for (String key : keys) {
			sb.append(key);
			sb.append(paramsMap.get(key));
		}
		sb.append(secretKey);
		return JhjEncryptUtil.MD5HexString(sb.toString());
	}

	/* 生成签名 */
	public static String getSign4RestAPI() {
		StringBuffer sb = new StringBuffer();
		sb.append(REST_API_ACCESS_PERMISSION);
		sb.append(secretKey);
		return JhjEncryptUtil.MD5HexString(sb.toString());
	}

	/* 生成签名 */
	public static String getAppKeyStr() {
		String keyStr=UUID.randomUUID().toString();
		return JhjEncryptUtil.MD5HexString(keyStr);
	}
}
